package com.zeedev.flyingsaucer.adapter;

import android.content.Context;
import android.database.Cursor;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Beers;
import mobi.zeedev.flyingsaucer.common.DataModule.Util;

public class BeerDialogCursorAdapter extends ResourceCursorAdapter {

	public BeerDialogCursorAdapter(
			Context context, 
			int layout, 
			Cursor c) {
		super(context, layout, c);
	}

	//nAdapter = new BeerDialogAdapter(this, R.layout.beer_dialog, mCursor);
	
//	textStyle = (TextView) this.findViewById(R.id.beer_dialog_style);
//	textBrewer = (TextView) this.findViewById(R.id.beer_dialog_brewer);
//	textDescription = (TextView)  this.findViewById(R.id.beer_dialog_description);
//	textBeerContainer = (ImageView)  this.findViewById(R.id.textBeerContainer);
//	textBeerName = (TextView) this.findViewById(R.id.textBeerName);
////	textCity = (TextView) this.findViewById(R.id.beer_dialog_city);
//	textCountry = (TextView) this.findViewById(R.id.beer_dialog_country);
	
//	dismissButton = (ImageView) this.findViewById(R.id.beer_dialog_dismiss);
//	dismissButton.setOnClickListener(dismissButtonListener);
	
//	mCursor.requery();
	/*ContentQueryMap ccc = new ContentQueryMap(mCursor, MetaBeer.BEER_ID, false, null);
	//Cursor c2 = app.getBeerDB().getFullBeerList();
	//this.startManagingCursor(c2);
	//ContentQueryMap ccc = new ContentQueryMap(c2, MetaBeer.BEER_ID, false, null);
	ContentValues cv = ccc.getValues(beerId.toString());

	textStyle.setText("Style: " + cv.getAsString(MetaBeer.STYLE_NAME));
	textBrewer.setText("Brewer: " + cv.getAsString(MetaBeer.BREWER_NAME));
	
	String originalDescription = cv.getAsString(MetaBeer.DESCRIPTION);
	Spanned descriptionText = Html.fromHtml(originalDescription);
	textDescription.setText( descriptionText );

	textBeerName.setText( cv.getAsString(MetaBeer.NAME));
	//textCity.setText();
	textCountry.setText(cv.getAsString(MetaBeer.CITY) + ", "  + cv.getAsString(MetaBeer.COUNTRY_NAME));
	
	//
	//textBeerContainer.setText(cv.get(MetaBeer.CONTAINER));	
    Boolean isCan = (cv.getAsInteger(MetaBeer.CAN)!=null ? cv.getAsInteger(MetaBeer.CAN) : 0) == 1 ? true : false;
    Boolean isDraught = (cv.getAsInteger(MetaBeer.DRAUGHT)!=null ? cv.getAsInteger(MetaBeer.DRAUGHT) : 0) == 1 ? true : false;
    Boolean isBottled = (cv.getAsInteger(MetaBeer.BOTTLE)!=null ? cv.getAsInteger(MetaBeer.BOTTLE) : 0) == 1 ? true : false;
    
    
    if (isCan) {
    	//textBeerContainer.setImageDrawable(drawable)
    	textBeerContainer.setImageDrawable( this.getResources().getDrawable(R.drawable.can) );
    } else
    if (isDraught) {
    	//textBeerContainer.setText("D");
    	textBeerContainer.setImageDrawable( this.getResources().getDrawable(R.drawable.draught) );
    } else
    if (isBottled) {
    	//textBeerContainer.setText("B");
    	textBeerContainer.setImageDrawable( this.getResources().getDrawable(R.drawable.bottle) );
    }*/
	
	
	
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		
		int descIndex = cursor.getColumnIndex(Beers.DESCRIPTION);
		String description = cursor.getString(descIndex);
//		ViewGroup vg = (ViewGroup) view.getParent();
//		ViewGroup curv = (ViewGroup) view.findViewById(R.id.beer_dialog_3_top_most_container);
//		curv.inflate(context, resource, root)
//		context.getResources().getSystem().

		ProgressBar pb = (ProgressBar) view.findViewById(R.id.beer_dialog_4_listview_progress_indicator);
		View descriptionContainer = view.findViewById(R.id.beer_dialog_4_listview_description_container);
		
		if ( TextUtils.isEmpty(description) ) {
////			progress = Generate.createProgressDialog(this, "Beer description unavailable...", "...getting description from sever.");
////			progress.show();
////			ImageView iv = new ImageView(context);
////			Drawable draw = context.getResources().getDrawable(android.R.drawable.progress_indeterminate_horizontal);
////			iv.setImageDrawable(draw);
////			view.set
//			for (int i = 0; i < vg.getChildCount(); i++) {
//				vg.getChildAt(i).setVisibility(View.INVISIBLE);
//			}
//			LayoutInflater lf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
////			lf.inflate(R.layout.loading,  curv);
//			lf.inflate(R.layout.loading, vg, true);
//
//			return;
			
			
			pb.setIndeterminate(true);
			pb.setVisibility(View.VISIBLE);
		} 
		else {
			pb.setVisibility(View.GONE);
		}
		
		
		TextView textStyle = null;
		TextView textBrewer = null;
		TextView textDescription = null;
		ImageView textBeerContainer = null;
		TextView textBeerName = null;
		TextView textCountry = null;
		ImageView dismissButton;
		
		textStyle = (TextView) view.findViewById(R.id.beer_dialog_style);
		textBrewer = (TextView) view.findViewById(R.id.beer_dialog_brewer);
		textDescription = (TextView)  view.findViewById(R.id.beer_dialog_description);
		textBeerContainer = (ImageView)  view.findViewById(R.id.textBeerContainer);
		textBeerName = (TextView) view.findViewById(R.id.textBeerName);
//		textCity = (TextView) this.findViewById(R.id.beer_dialog_city);
		textCountry = (TextView) view.findViewById(R.id.beer_dialog_country);
		
		dismissButton = (ImageView) view.findViewById(R.id.beer_dialog_dismiss);
//		dismissButton.setOnClickListener(dismissButtonListener);
		
//		ContentQueryMap ccc = new ContentQueryMap(cursor, Beers.BEER_ID, false, null);
//		ContentValues cv = ccc.getRows().values().toArray(new ContentValues[] {})[0];
		String style = Util.getFromCursor(cursor, Beers.STYLE_NAME, String.class);
		String brewer = Util.getFromCursor(cursor, Beers.BREWER_NAME, String.class);
		style = style != null ? "Style: " + style : "";
		brewer = brewer != null ? "Brewer: " + brewer : "";
		textStyle.setText(style);	
		
		
		textBrewer.setText(brewer);
		
		textDescription.setMovementMethod(new ScrollingMovementMethod());
		textDescription.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
		
		String originalDescription = Util.getFromCursor(cursor, Beers.DESCRIPTION, String.class);
		if (!TextUtils.isEmpty(originalDescription)) {
			Spanned descriptionText = Html.fromHtml(originalDescription);
			textDescription.setText( descriptionText );
			descriptionContainer.setVisibility(View.VISIBLE);

		} else {
			descriptionContainer.setVisibility(View.GONE);
			textDescription.setText( "" );
		}
		
		String city = Util.getFromCursor(cursor, Beers.CITY, String.class);
		String country = Util.getFromCursor(cursor, Beers.COUNTRY_NAME, String.class);
		String name = Util.getFromCursor(cursor, Beers.NAME, String.class);
		String cityCountry = "";
//			!TextUtils.isEmpty(city) ^ !TextUtils.isEmpty(country) ? city + ", "  + country : "";
		if (!TextUtils.isEmpty(city)) {
			cityCountry += city;
		}
		if (!TextUtils.isEmpty(cityCountry) && !TextUtils.isEmpty(country)) {
			cityCountry += ", " + country;
		}
		else if (!TextUtils.isEmpty(country)) {
			cityCountry += country;
		}
		
		name = name != null ? name : "";
			textBeerName.setText( name );
		
		//textCity.setText();
		textCountry.setText( cityCountry );
		
		//
		//textBeerContainer.setText(cv.get(MetaBeer.CONTAINER));	
	    Boolean isCan = (Util.getFromCursor(cursor, Beers.CAN, int.class)!=null ? Util.getFromCursor(cursor, Beers.CAN, int.class) : 0) == 1 ? true : false;
	    Boolean isDraught = (Util.getFromCursor(cursor, Beers.DRAUGHT, int.class)!=null ? Util.getFromCursor(cursor, Beers.DRAUGHT, int.class) : 0) == 1 ? true : false;
	    Boolean isBottled = (Util.getFromCursor(cursor, Beers.BOTTLE, int.class)!=null ? Util.getFromCursor(cursor, Beers.BOTTLE, int.class) : 0) == 1 ? true : false;
	    
	    
	    if (isCan) {
	    	//textBeerContainer.setImageDrawable(drawable)
	    	textBeerContainer.setImageDrawable( context.getResources().getDrawable(R.drawable.can) );
	    }
	    else if (isDraught) {
	    	//textBeerContainer.setText("D");
	    	textBeerContainer.setImageDrawable( context.getResources().getDrawable(R.drawable.draught) );
	    }
	    else if (isBottled) {
	    	//textBeerContainer.setText("B");
	    	textBeerContainer.setImageDrawable( context.getResources().getDrawable(R.drawable.bottle) );
	    }
	    else {
	    	textBeerContainer.setImageDrawable( null );
	    }
	    
	    
	    
	    
	    
	}

	
	
	
	
}
